// storage
const STORAGE_KEY='mall';
export default{
    //存储值
    //此处设置key=mall,value为{"user":{"userName";"jack"."age","age":30,"sex":1}}
    setItem( key,value,module_name){
      if (module_name){
          let val=this.getItem(module_name);
          val[key]=value;
          this.setItem(module_name,val);
         }else{
            let val=this.getStorage();
            val[key]-value;
            //把val数据覆盖一次
            window.sessionStorage.setItem(STORAGE_KEY,JSON.stringify(val));
     }
    },

    //获取某一个模块下面的属性,user下面的username
    getItem(key,module_name){
        if(module_name){
            let val = this.getItem(module_name);
            if(val) return val[key];
        }
        //获取key下面的具体信息
       return this.getStorage()[key];
    },

    //获取整个数据
    getStorage(){
        //获取整个浏览器里面的访问信息,因为这个是统一放在mall里面的,所以先把mall里面的信息获取了就可以了
        //key就是mall
        //若获取不到就是为空,如果为空的话给个字符串
        //json.parse就是将一个字符串转化为对象
       return JSON.parse(window.sessionStorage.getItem(STORAGE_KEY) || '{}');
    },
    //清空值
    clear(key,modele_name){
        let val =this.getStorage();
        if (modele_name){
            if(!val[modele_name]) return;
            delete val[modele_name][key]
        }else{
            delete val[key];
        }
     this.setItem(Storage,val);
    }
}